package com.huawei.hwCloudJs.core;

import android.util.Log;
import android.webkit.WebView;
import com.huawei.hms.framework.network.restclient.hianalytics.HianalyticsData;
import com.huawei.hwCloudJs.support.enables.INoProguard;
import java.lang.ref.WeakReference;
import org.json.JSONException;
import org.json.JSONObject;

public class JsCallback implements INoProguard {
    private static final int a = 0;
    private static final int b = 9999;
    private static final String c = "JsCallback";
    private static final String d = "javascript:%s.callback(%s,%s,%d);";
    private final String e;
    private WeakReference<WebView> f;
    private String g;
    private String h;

    public JsCallback(WebView webView, String str, String str2) {
        this.g = str2;
        this.f = new WeakReference(webView);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("\"");
        stringBuilder.append(str);
        stringBuilder.append("\"");
        this.e = stringBuilder.toString();
        this.h = str;
    }

    private String a(String str) {
        return str;
    }

    private void a(WebView webView, String str) {
        webView.post(new a(webView, str));
    }

    private void a(String str, int i) {
        WebView webView = getWebView();
        if (webView != null) {
            a(webView, String.format(d, new Object[]{"hbs", this.e, str, Integer.valueOf(i)}));
        }
    }

    public final void failure() {
        failure(9999);
    }

    public final void failure(int i) {
        failure(i, "");
    }

    public final void failure(int i, String str) {
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("code", i);
            jSONObject.put(HianalyticsData.EXCEPTION_MESSAGE, str);
        } catch (JSONException e) {
            Log.e(c, "failure put json error");
        }
        a(a(jSONObject.toString()), i);
    }

    public final void failure(String str) {
        failure(9999, str);
    }

    public WebView getWebView() {
        return (WebView) this.f.get();
    }

    public String getwebviewId() {
        return this.g;
    }

    public final void success() {
        success("{}");
    }

    public final void success(Object obj) {
        a(new b().a(obj), 0);
    }

    public final void success(String str) {
        a(a(str), 0);
    }
}
